package com.example.service;

import com.example.entity.Account;
import com.example.entity.Likes;
import com.example.mapper.LikesMapper;
import com.example.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LikesService {
    @Resource
    private LikesMapper likesMapper;

    /**
     * 点赞或取消点赞
     * @param likes
     */
    public void set(Likes likes){
        Account currentUser = TokenUtils.getCurrentUser();
        likes.setUser_id(currentUser.getId());
        Likes dblLikes = likesMapper.selectUserLikes(likes);
        System.out.println("喜欢的次数是多少次" + dblLikes);
        if(dblLikes == null){
            likesMapper.insert(likes);
        }else{
            System.out.println("要删除的id是" + dblLikes.getId()) ;
            likesMapper.deleteById(dblLikes.getId());
        }
    }

    /**
     * 查询当前用户是否点过赞
     * 点过赞的时候就会返回null不是点赞之后不能返回null
     */
    public Likes selectUserLikes(Integer fid, String modules){
        Account currentUser = TokenUtils.getCurrentUser();
        Likes likes = new Likes();
        likes.setUser_id(currentUser.getId());
        likes.setFid(fid);
        likes.setModule(modules);
        return likesMapper.selectUserLikes(likes);
    }


    public int selectByFidAndModule(Integer fid, String module){
        return likesMapper.selectByFidAndModule(fid,module);
    }
}
